﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TierApplication;
public partial class submenu : System.Web.UI.Page
{
    P_Moudle _Moudle = new P_Moudle();
    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            //Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(Validate), null);

            if (Request["mid"] != null)
            {
                string strUrl = string.Empty;
                string strmid = Request["mid"].ToString();
                //strUrl = "~/" + strmid + ".aspx";
                //this.SiteMapDataSource2.StartingNodeUrl = strUrl;
                InitTree(this._Moudle.GetMoudleByRole(Authentication.GetUserRole()), strmid);
                //InitTree(this._Moudle.GetMoudleAll(), strmid);

            }
        }

    }
    /// <summary>
    /// 加载数据
    /// </summary>
    private void InitTree(List<MoudleInfo> data,string id)
    {
        this.TreeView1.Nodes.Clear();
        List<MoudleInfo> ListInfo = data.FindAll(delegate(MoudleInfo info)
        {
            return info.ParentId.ToString() == id;
        });
        foreach (MoudleInfo info in ListInfo)
        {
            TreeNode node = new TreeNode();
            node.Text = info.MoudleName;
            node.Value = info.MoudleId;
            node.NavigateUrl = info.Url + "?MOUDLEID=" + info.MoudleId;
            node.Target = "MainFrame";
            node.Expanded = true;
            this.TreeView1.Nodes.Add(node);
            AddReplies(data, node);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="node"></param>
    private void AddReplies(List<MoudleInfo> data, TreeNode node)
    {
        List<MoudleInfo> ListInfo = data.FindAll(delegate(MoudleInfo info)
        {
            return info.ParentId.ToString() == node.Value;
        });
        foreach (MoudleInfo info in ListInfo)
        {
            TreeNode replyNode = new TreeNode();
            replyNode.Text = info.MoudleName;
            replyNode.Value = info.MoudleId;
            replyNode.NavigateUrl = info.Url + "?MOUDLEID=" + info.MoudleId;
            replyNode.Target = "MainFrame";
            replyNode.Expanded = true;
            node.ChildNodes.Add(replyNode);
            AddReplies(data, replyNode);
        }
    }

    public void Validate(HttpContext context, Object data, ref HttpValidationStatus status)
    {
        object obj = Session["RemoveSubMenuCach"];

        bool isV = true;
        //if(obj!=null && obj.GetType().Equals(typeof(Boolean)))
        if (obj != null)
        {
            isV = (bool)obj;
            if (isV == true)
            {
                status = HttpValidationStatus.Invalid;
                try
                {
                    Session.Remove("RemoveSubMenuCach");
                }
                finally
                {

                }
            }
            else
            {
                status = HttpValidationStatus.Valid;
            }
        }
        else
        {
            status = HttpValidationStatus.Valid;
        }

    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
    {
        //节点
        SiteMapNode smn = (SiteMapNode)e.Node.DataItem;
        string strOpID = smn.ResourceKey;
        //Epower.ITSM.SqlDAL.UIMethod ui = new Epower.ITSM.SqlDAL.UIMethod();

        string strTarget = smn["target"];

        if (strTarget != "")
        {
            e.Node.Target = strTarget;
        }


        //if (strOpID != null && strOpID.Length > 0)
        //    ui.CheckNodeRight(strOpID, e.Node, (Hashtable)Session["UserAllRights"],TreeView1);


    }



    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void TreeView1_DataBound(object sender, EventArgs e)
    {
        //将所有子菜单项全部移除的父菜单项移除
        ClearNoSubItems1();

        //System.IO.StringWriter tw = new System.IO.StringWriter();
        //System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

        //TreeView1.RenderControl(hw);

        //Session["CashedMenuHtml1"] = hw.ToString();



    }

    /// <summary>
    /// 将所有子菜单项全部移除的父菜单项移除
    /// </summary>
    private void ClearNoSubItems1()
    {
        //把没有子菜单的第一级菜单删除


        TreeView lmenu = TreeView1;
        for (int i = lmenu.Nodes.Count - 1; i > -1; i--)
        {
            TreeNode item = lmenu.Nodes[i];
            ClearNoSubItems1(item);
            //如果下级删除完毕，则删除自己
            if (item.ChildNodes.Count == 0 && item.NavigateUrl == "")
            {
                TreeView1.Nodes.Remove(item);
            }
        }

    }

    /// <summary>
    /// 递归检查是否有子菜单，如果没有子菜单，则移除父菜单
    /// </summary>
    /// <param name="pitem"></param>
    private void ClearNoSubItems1(TreeNode pitem)
    {
        TreeNode litem = pitem;
        //递归菜单
        for (int i = 0; i < litem.ChildNodes.Count; i++)
        {
            ClearNoSubItems1(pitem.ChildNodes[i]);
        }
        //foreach (TreeNode item in pitem.ChildNodes)
        //{
        //    ClearNoSubItems1(item);
        //}
        //如果没有子菜单，则移除父菜单
        if (pitem.ChildNodes.Count == 0 && pitem.NavigateUrl == "")
        {
            if (pitem.Parent != null)
                pitem.Parent.ChildNodes.Remove(pitem);
        }
    }
}
